(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.sitetplRoleEditController', sitetplRoleEditController);
	sitetplRoleEditController.$inject = [ '$scope', '$timeout', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig' , 'authService', 'ajaxServiceFactory', 'validatorConfig', 'arithService'];
	function sitetplRoleEditController($scope, $timeout, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, authService, ajaxServiceFactory, validatorConfig, arithService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("sitetplRoleEditController"), ajaxService = ajaxServiceFactory.buildAjaxService({
		});
		ctrl.dialogsModule = dialogsModule;
//		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.sitetplRole = {};
		ctrl.filterRoleNames = ctrl.dialogsModule.params.filterRoleNames;
		ctrl.formName = "sitetplRoleForm";
		
		ctrl.init = function() {
			if (ctrl.validatorConfig.sitetplRoles)
				layoutConfig.formConfigs.sitetplRoleRight.tableConfigs.table.validatorConfig = ctrl.validatorConfig.sitetplRoles.sitetplRoleRights;
			ctrl.sitetplRoleRightInnerGridOptions = gridService.createInnerGridOptions($scope, layoutConfig.formConfigs.sitetplRoleRight.tableConfigs.table, ctrlProfLimitFaciAttrRowSelectionChanged);
			ctrl.sitetplRoleRightInnerGridOptions.data = 'vm.sitetplRole.sitetplRoleRights';
			function ctrlProfLimitFaciAttrRowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedProfLimitFaciAttrs = selectedRows;
			}
			// init general
			initGeneral(ctrl);
			var initSitetplRole = ctrl.dialogsModule.params.sitetplRole || {};
			ctrl.layoutConfig = layoutConfig;
			$timeout(function() {
				ctrl.sitetplRole = initSitetplRole;
			});
		};
		
		function initGeneral(ctrl) {
		}
		
		ctrl.roleNameChanged = function(value) {
			if (ctrl.sitetplRole.roleName && ctrl.filterRoleNames && ctrl.filterRoleNames.length > 0) {
				for (var i = 0; i < ctrl.filterRoleNames.length; i++) {
					var filterRoleName = ctrl.filterRoleNames[i];
					if (ctrl.sitetplRole.roleName == filterRoleName) {
						// changed eventCode has used
						delete ctrl.sitetplRole.roleName;
						modalService.openErrorDialogs({
							messages : [ 'js.info.sitetplrole.duplicate-rolename' ]
						});
						return;
					}
				}
			}
		};
		
		ctrl.addSitetplRoleRight = function() {
			return $q(function(resolved, rejected){
				var params = {};
				params.filterRightCodes = [];
				angular.forEach(ctrl.sitetplRole.sitetplRoleRights, function(sitetplRoleRight, index) {
					params.filterRightCodes.push(sitetplRoleRight.rightCode);
				});
				params.multiSelect = true;
				modalService.openCustomDialogs({
					'dialogsModule' : 'cda.right',
					'params' : params,
					'btnCallback' : {
						'ok' : function(selectedRows) {
							if (!selectedRows || selectedRows.length == 0) {
								modalService.openErrorDialogs({
									messages : [ 'title.dialogs.info.norecsel' ]
								});
								return false;
							}
							if (!ctrl.sitetplRole.sitetplRoleRights) {
								ctrl.sitetplRole.sitetplRoleRights = [];
							}
							angular.forEach(selectedRows, function(selectedObj, index) {
								ctrl.sitetplRole.sitetplRoleRights.push(selectedRows[index]);
							});
						}
					}
				});
				rejected();
			});
		};
		
		
		function validateSitetplRoleRight(ctrl) {
			if (!ctrl.sitetplRole.sitetplRoleRights || ctrl.sitetplRole.sitetplRoleRights.length == 0) {
				var msg = 'Role must have rights.';
				return msg;
			}
		}
		
		ctrl.ok = function() {
			if ($scope.sitetplRoleForm) {
				$scope.sitetplRoleForm.$setSubmitted();
				if(!$scope.sitetplRoleForm.$valid) {
					dialogsModule.showErrorMessages($scope.sitetplRoleForm);
					return;
				}
			}
			// if such as length no pass, model will be undefined always 
			var messages = [];
			var msg1 = validateSitetplRoleRight(ctrl);
			if (msg1) messages.push(msg1);
			if (messages && messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
				return;
			}
			if ((!$scope.sitetplRoleForm || $scope.sitetplRoleForm.$valid) && (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.sitetplRole) !== false)) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);
